# -*- coding: UTF-8 -*-
from urllib import request
import re

if __name__ == "__main__":
    # 访问网址获取IP
    url = 'https://www.whatismyip.com/ip-address-lookup/'

    header = {
        "User-Agent": " Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11",
    }

    # 构建request对象
    request_url = request.Request(url, headers=header)

    # 发送请求获取返回数据
    response = request.urlopen(request_url)

    # 读取相应信息并解码，并利用正则提取IP
    html = response.read().decode("utf-8")
    print(html)
    pattern = re.compile(r'\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b')
    ip_list = re.findall(pattern, html)

    print(ip_list[0])
